﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using IHISCETeim.Model;
using IHISCETeim.BLL;
using IHISCETeim.Library;
using FineUI;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.Data;
using System.Text;

namespace IHISCETeim.Web.AgentSys.SvrMgr
{
    public partial class ReportMainFault : AgentPageBase
    {

        protected override Permission[] PowerKey
        {
            get
            {
                return new Permission[] { Permission.查看 };
            }
        }
        protected override string DWZRef
        {
            get { return "ReportMainFault"; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.FromDate.Text = DateTime.Now.ToString("yyyy-MM-01");
                this.ToDate.Text = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-01")).AddMonths(1).AddDays(-1).ToString("yyyy-MM-dd");
            }
        }

        private void binddata()
        {
            //    this.rpData.DataSoure = dt;
            //    this.rpData.DataBind();
        }

        protected DataTableCollection _dt = null;
        protected DataTableCollection dt
        {
            get
            {
                if (_dt == null)
                {
                    _dt = AgentQltyReportController.GetMainFault(this.FromDate.Text, this.ToDate.Text);
                }
                return _dt;
            }
        }

        protected string GetShowTable()
        {
            DataTable dtHead = dt[0];
            DataTable dtbody = dt[1];
            DataTable dtrowmax = dt[2];
            int maxrownum = 0;
            try
            {
                maxrownum = Convert.ToInt32(dtrowmax.Rows[0][0]);
            }
            catch
            {
                maxrownum = 0;
                return "";
            }


            DataTable dttemp = new DataTable();
            foreach (DataRow dr in dtHead.Rows)
            {
                DataColumn dc = new DataColumn(dr["ParTitle"].ToString() + dr["Num"].ToString()+"例");
                dttemp.Columns.Add(dc);
            }
            for (int rowindex = 0; rowindex < maxrownum; rowindex++)
            {
                dttemp.Rows.Add(dttemp.NewRow());
            }

            foreach (DataRow dr in dtHead.Rows)
            {
                DataTable sdt = dtbody.Select(string.Format("parid={0}", dr["ParId"])).CopyToDataTable();
                int rowtemp = 0;
                foreach (DataRow sdr in sdt.Rows)
                {
                    dttemp.Rows[rowtemp][dr["ParTitle"].ToString() + dr["Num"].ToString() + "例"] = sdr["Title"] + " " + sdr["Num"] + "例";
                    rowtemp++;
                }
            }

            StringBuilder sb = new StringBuilder();
            sb.Append("<thead>");
            foreach (DataColumn dc in dttemp.Columns)
            {
                sb.AppendFormat("<th>{0}</th>", dc.ColumnName);
            }
            sb.Append("</thead>");

            sb.Append("<tboby>");
            foreach (DataRow dr in dttemp.Rows)
            {
                sb.Append("<tr>");
                
                foreach (DataColumn dc in dttemp.Columns)
                {
                    sb.AppendFormat("<td>{0}</td>", dr[dc.ColumnName]);
                }
                sb.Append("</tr>");
            }
            sb.Append("</tboby>");

            return sb.ToString();

        }

        protected string GetCharData()
        {
            DataTable dtHead = dt[0];
            StringBuilder sb = new StringBuilder();
            foreach (DataRow dr in dtHead.Rows)
            {
                sb.Append("<tr>");
                sb.AppendFormat("<td>{0}</td><td>{1}</td>", dr["ParTitle"],dr["Num"]);
                sb.Append("</tr>");
            }

            return   sb.ToString();

        }
       


        protected void btnSearch_Click(object sender, EventArgs e)
        {

        }
    }
}